package com.jgsu.farm.controller;

import com.jgsu.farm.entity.Message;
import com.jgsu.farm.exception.*;
import com.jgsu.farm.service.MessageService;
import com.jgsu.farm.utils.CommonResult;
import com.jgsu.farm.utils.ResultCode;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Slf4j
@Api(value = "farm/sysuser/message", tags = "资讯信息管理模块")  //描述信息用的，标注在类上
@RestController
@RequestMapping("farm/sysuser/message")
public class MessageController {
    @Autowired
    MessageService messageService;

    /**
     * 资讯添加
     */
    @ApiOperation(value = "资讯添加功能")
    @PostMapping(value = "/add", produces = "application/json")
    public CommonResult messageAdd(Message message) {
        try {
            messageService.add(message);
            log.info("资讯添加成功，植保知识title为：{}", message.getTitle());
            return CommonResult.success(ResultCode.SUCCESS);
        } catch (DataAddException e) {
            log.error("资讯添加异常,title为：{}", message.getTitle());
            throw new DataAddException(e.getCode(), e.getMessage());
        }
    }

    /**
     * 资讯删除
     */
    @ApiOperation(value = "资讯删除功能")
    @DeleteMapping(value = "/delete", produces = "application/json")
    public CommonResult messageDelete(@RequestParam(value = "messid") Integer messid) {
        try {
            messageService.delete(messid);
            log.info("删除id为{}的资讯信息", messid);
            return CommonResult.success(ResultCode.SUCCESS);
        } catch (DataNotExistException e) {
            log.error("不存在id为:{}的资讯信息，删除失败", messid);
            throw new DataNotExistException(e.getCode(), e.getMessage());
        }
    }

    /**
     * 资讯查询
     */
    @ApiOperation(value = "资讯知识查询功能")
    @GetMapping(produces = "application/json")
    public CommonResult<PageInfo<Message>> messageList(@RequestParam(value = "page", required = false, defaultValue = "1") int page,
                                                       @RequestParam(value = "pagesize", required = false, defaultValue = "10") int pagesize) {
        try {
            PageInfo<Message> messages = messageService.selectAll(page, pagesize);
            log.info("查询所有的资讯信息");
            return CommonResult.success(messages);
        } catch (DataNotExistException e) {
            log.error("资讯信息表中没有数据");
            throw new DataNotExistException(e.getCode(), e.getMessage());
        }
    }

    /**
     * 资讯修改
     */
    @ApiOperation(value = "资讯修改功能")
    @PutMapping(value = "/update", produces = "application/json")
    public CommonResult messageUpdate(Message message) {
        try {
            messageService.update(message);
            log.info("更新title为{}的资讯信息成功", message.getTitle());
            return CommonResult.success(message);
        } catch (DataNotExistException e) {
            log.error("更新title为{}的资讯信息失败", message.getTitle());
            throw new DataNotExistException(e.getCode(), e.getMessage());
        }
    }
}
